import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:number_spark_flutter/pages/ns_home/single/ns_single_logic.dart';
import 'package:number_spark_flutter/style/ns_style.dart';
import 'package:number_spark_flutter/widgets/brick/ns_brick.dart';

import 'ns_double_logic.dart';

class NSDoublePage extends StatelessWidget {
  NSDoublePage({Key? key}) : super(key: key);

  final logic = Get.put(NSDoubleLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          const NSImage(
            assetImage: 'img_bg_main.png',
            width: double.infinity,
            height: double.infinity,
            fit: BoxFit.fill,
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              NSRow(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                margin: EdgeInsets.only(top: ScreenUtil().statusBarHeight + 14.w, left: 20.w, right: 24.w),
                children: [
                  NSImage(
                    assetImage: 'icon_red_close.png',
                    width: 40.w,
                    onTap: () {
                      Get.back();
                    },
                  ),
                  GetBuilder<NSDoubleLogic>(
                    builder: (logic) {
                      return NSText(
                        text: '${logic.countDown}S',
                        textColor: Colors.white,
                        fontSize: 24.sp,
                        fontFamily: 'LilitaOne-Regular',
                        marginDrawable: 8.w,
                        startDrawable: NSImage(
                          assetImage: 'icon_timer.png',
                          width: 32.w,
                        ),
                      );
                    },
                    id: idCountDown,
                  ),
                ],
              ),
              IntrinsicHeight(
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    NSImage(
                      margin: EdgeInsets.only(left: 18.w, right: 18.w, top: 0.w),
                      assetImage: 'img_bg_double.png',
                      height: 698.h,
                    ),
                    GetBuilder<NSDoubleLogic>(
                      builder: (logic) {
                        return Stack(
                          alignment: Alignment.center,
                          children: [
                            NSImage(
                              assetImage: 'img_bg_double_center.png',
                              width: 363.w,
                            ),
                            NSRow(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                NSText(
                                  text: logic.questionsList.where((element) => element.myAnswer == element.answer).length.toString(),
                                  textColor: Colors.white,
                                  fontSize: 24.sp,
                                  fontFamily: 'LilitaOne-Regular',
                                ),
                                SizedBox(width: 80.w),
                                NSText(
                                  text: logic.questionsList.where((element) => element.yourAnswer == element.answer).length.toString(),
                                  textColor: Colors.white,
                                  fontSize: 24.sp,
                                  fontFamily: 'LilitaOne-Regular',
                                ),
                              ],
                            )
                          ],
                        );
                      },
                      id: idCorrectCount,
                    ),
                    NSColumn(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      width: 363.w,
                      height: 698.h,
                      children: [
                        Expanded(
                          child: Transform.rotate(
                            angle: pi,
                            child: GetBuilder<NSDoubleLogic>(
                                id: idYourQuestion,
                                builder: (logic) {
                                  return NSColumn(
                                    children: [
                                      NSText(
                                        margin: EdgeInsets.only(top: 47.w),
                                        text: logic.questionsList[logic.yourIndex].expression,
                                        textColor: kAppColor('#7131F2'),
                                        fontFamily: 'LilitaOne-Regular',
                                        fontSize: 40.sp,
                                      ),
                                      GridView.builder(
                                          shrinkWrap: true,
                                          padding: EdgeInsets.only(left: 52.w, right: 52.w, top: 33.w),
                                          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2, crossAxisSpacing: 18.w, mainAxisSpacing: 10.w, childAspectRatio: 128 / 74),
                                          itemBuilder: (context, index) {
                                            return NSStack(
                                              alignment: Alignment.center,
                                              children: [
                                                NSImage(
                                                  assetImage: 'img_bg_answer_btn.png',
                                                  width: 128.w,
                                                ),
                                                NSText(
                                                  text: logic.questionsList[logic.yourIndex].options[index].toString(),
                                                  textColor: kAppColor('#7131F2'),
                                                  fontFamily: 'LilitaOne-Regular',
                                                  fontSize: 32.sp,
                                                ),
                                              ],
                                              onTap: () {
                                                logic.doClickYourOption(logic.questionsList[logic.yourIndex].options[index]);
                                              },
                                            );
                                          },
                                          itemCount: logic.questionsList[logic.yourIndex].options.length)
                                    ],
                                  );
                                }),
                          ),
                        ),
                        SizedBox(height: 73.h),
                        Expanded(
                          child: GetBuilder<NSDoubleLogic>(
                            builder: (logic) {
                              return NSColumn(
                                children: [
                                  NSText(
                                    margin: EdgeInsets.only(top: 39.w),
                                    text: logic.questionsList[logic.myIndex].expression,
                                    textColor: kAppColor('#7131F2'),
                                    fontFamily: 'LilitaOne-Regular',
                                    fontSize: 40.sp,
                                  ),
                                  GridView.builder(
                                      shrinkWrap: true,
                                      padding: EdgeInsets.only(left: 52.w, right: 52.w, top: 33.w),
                                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2, crossAxisSpacing: 18.w, mainAxisSpacing: 10.w, childAspectRatio: 128 / 74),
                                      itemBuilder: (context, index) {
                                        return NSStack(
                                          alignment: Alignment.center,
                                          children: [
                                            NSImage(
                                              assetImage: 'img_bg_answer_btn.png',
                                              width: 128.w,
                                            ),
                                            NSText(
                                              text: logic.questionsList[logic.myIndex].options[index].toString(),
                                              textColor: kAppColor('#7131F2'),
                                              fontFamily: 'LilitaOne-Regular',
                                              fontSize: 32.sp,
                                            ),
                                          ],
                                          onTap: () {
                                            logic.doClickMyOption(logic.questionsList[logic.myIndex].options[index]);
                                          },
                                        );
                                      },
                                      itemCount: logic.questionsList[logic.myIndex].options.length)
                                ],
                              );
                            },
                            id: idMyQuestion,
                          ),
                        )
                      ],
                    )
                  ],
                ),
              )
            ],
          )
        ],
      ),
    );
  }
}
